//
//  vechileDetailViewController.m
//  WHB_logist
//
//  Created by 徐国庆 on 15/12/23.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "vechileDetailViewController.h"
#import "CWStarRateView.h"


@interface vechileDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)UITableView    *tableView;       // 列表
@property(nonatomic, strong)licensePlateList *licensePlate;

@end

@implementation vechileDetailViewController

- (instancetype)initWithLicensePlateList:(licensePlateList *)licensePlate{
    self = [super init];
    if (self) {
        _licensePlate = licensePlate;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navtitle.text = @"车辆详情";
    [self initWithView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"vechileDetailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    float height = 25;
    
    // 最上边的虚线框
    UILabel *topLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 10)];
    topLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    topLineLabel.layer.borderWidth = 0.5;
    topLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"9D9D9D"].CGColor;
    [cell.contentView addSubview:topLineLabel];
    
    // 车牌号
    UIView *vechileNumberView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(topLineLabel.frame), kUIScreenWidth - 20, height)];
    [vechileNumberView addSubview:[self titleAndContent:@"车牌号:" andContent:_licensePlate.licensePlate withToAddress:nil]];
    [cell.contentView addSubview:vechileNumberView];
    
    // 姓名
    UIView *nameView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(vechileNumberView.frame), kUIScreenWidth - 20, height)];
    [nameView addSubview:[self titleAndContent:@"姓名" andContent:_licensePlate.driverName withToAddress:nil]];
    [cell.contentView addSubview:nameView];
    
    // 服务评价
    UIView *severEvaluateView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(nameView.frame), kUIScreenWidth - 20, height)];
    [severEvaluateView addSubview:[self fiveCellView:@"服务评价" withStar:2]];
    [cell.contentView addSubview:severEvaluateView];
    
    // 服务次数
    UIView *severCountView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(severEvaluateView.frame), kUIScreenWidth - 20, height)];
    [severCountView addSubview:[self titleAndContent:@"服务次数:" andContent:_licensePlate.truckEvaluationTimes withToAddress:nil]];
    [cell.contentView addSubview:severCountView];
    
    // 服务次数下的虚线框
    UILabel *severCountBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(severCountView.frame) + 10, kUIScreenWidth, 10)];
    severCountBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    severCountBottomLineLabel.layer.borderWidth = 0.5;
    severCountBottomLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"9D9D9D"].CGColor;
    [cell.contentView addSubview:severCountBottomLineLabel];
    
    // 路线
    UIView *roadLineView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(severCountBottomLineLabel.frame), kUIScreenWidth - 20, height)];
    [roadLineView addSubview:[self titleAndContent:@"路线:" andContent:_licensePlate.startAddressName withToAddress:_licensePlate.endAddressName]];
    [cell.contentView addSubview:roadLineView];
    
    // 位置
    UIView *addressView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(roadLineView.frame), kUIScreenWidth - 20, height)];
    [addressView addSubview:[self titleAndContent:@"位置:" andContent:@"哈尔滨南岗区西大直街318号6楼" withToAddress:nil]];
    [cell.contentView addSubview:addressView];
    
    // 距离
    UIView *distanceView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(addressView.frame), kUIScreenWidth - 20, height)];
    [distanceView addSubview:[self titleAndContent:@"距离:" andContent:@"1公里" withToAddress:nil]];
    [cell.contentView addSubview:distanceView];
    
    // 距离下的线
    UILabel *distanceBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(distanceView.frame) + 10, kUIScreenWidth, 10)];
    distanceBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
    distanceBottomLineLabel.layer.borderWidth = 0.5;
    distanceBottomLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"9D9D9D"].CGColor;
    [cell.contentView addSubview:distanceBottomLineLabel];

    // 货车图片
    UIImageView *vechileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(distanceBottomLineLabel.frame) + 10, 100, 100)];
    vechileImageView.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:vechileImageView];
    
    // 车辆品牌
    UIView *vechileBrandView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(vechileImageView.frame), CGRectGetMaxY(distanceBottomLineLabel.frame), kUIScreenWidth - 20, height)];
    [vechileBrandView addSubview:[self titleAndContent:@"车辆品牌:" andContent:_licensePlate.brandName withToAddress:nil]];
    [cell.contentView addSubview:vechileBrandView];
    
    // 运输介质
    UIView *mediumView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(vechileImageView.frame), CGRectGetMaxY(vechileBrandView.frame), kUIScreenWidth - 20, height)];
    [mediumView addSubview:[self titleAndContent:@"运输介质:" andContent:_licensePlate.mediumName withToAddress:nil]];
    [cell.contentView addSubview:mediumView];
    
    // 容积
    UIView *volumeView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(vechileImageView.frame), CGRectGetMaxY(mediumView.frame), kUIScreenWidth - 20, height)];
    [volumeView addSubview:[self titleAndContent:@"罐体容积" andContent:_licensePlate.volume withToAddress:nil]];
    [cell.contentView addSubview:volumeView];
    
    // 载重
    UIView *weightView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(vechileImageView.frame), CGRectGetMaxY(volumeView.frame), kUIScreenWidth - 20, height)];
    [weightView addSubview:[self titleAndContent:@"载重:" andContent:_licensePlate.weight withToAddress:nil]];
    [cell.contentView addSubview:weightView];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350;
}


#pragma mark 拨打电话事件
- (void)callingButtonAction:(UIButton *)button{
    NSLog(@"拨打电话");
}

#pragma mark 发布空车事件
- (void)publishEmptyVechileButtonAction:(UIButton *)button{
    NSLog(@"发布空车");
}


/**
 *  初始化视图
 */
- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kNavHeight) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [self footerView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

#pragma mark 自定义第一个、第四个cell
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    if ([contentLabel.text isEqualToString:@"已确认"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"8CEA00"];
    } else if ([contentLabel.text isEqualToString:@"未确认"]){
        contentLabel.textColor = [UIColor HexStrWithColor:@"FF2D2D"];
    }
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"地址:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width + 5, contentLabel.frame.origin.y + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 5, contentLabel.frame.origin.y, 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}

#pragma mark 自定义第五个cell
- (UIView *)fiveCellView:(NSString *)title withStar:(float)starSevel{
    
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    [bottomView addSubview:titleLabel];
    
    // 星
    CWStarRateView *ratingControl = [[CWStarRateView alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, titleLabel.frame.origin.y, 110, 20)];
    [bottomView addSubview:ratingControl];
    ratingControl.scorePercent = starSevel;
    ratingControl.hasAnimation = YES;
    
    return bottomView;
}

- (UIView *)footerView{
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth , 40)];
    
    // 拨打电话
    UIButton *callingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    callingButton.frame = CGRectMake(10, 0, (kUIScreenWidth - 30) / 2, 40);
    callingButton.layer.masksToBounds = YES;
    callingButton.layer.cornerRadius = 5;
    [callingButton setTitle:@"拨打电话" forState:UIControlStateNormal];
    callingButton.backgroundColor = [UIColor HexStrWithColor:@"CE0000"];
    [callingButton addTarget:self action:@selector(callingButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:callingButton];
    
    //发布空车
    UIButton *publishEmptyVechileButton = [UIButton buttonWithType:UIButtonTypeCustom];
    publishEmptyVechileButton.frame = CGRectMake((kUIScreenWidth - 30) / 2 + 20, 0, (kUIScreenWidth - 30) / 2, 40);
    publishEmptyVechileButton.layer.masksToBounds = YES;
    publishEmptyVechileButton.layer.cornerRadius = 5;
    [publishEmptyVechileButton setTitle:@"发布空车" forState:UIControlStateNormal];
    publishEmptyVechileButton.backgroundColor = [UIColor HexStrWithColor:@"CE0000"];
    [publishEmptyVechileButton addTarget:self action:@selector(publishEmptyVechileButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:publishEmptyVechileButton];

    return bottomView;
}

/**
 *  请求数据
 */
- (void)loadData{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
